//分页显示DataTable
var table3;

$(document).ready(function () {
    //日期插件初始化
    $('#datetimeStart').datetimepicker({
        language:  'zh-CN',
        format:'yyyy-mm-dd',
        weekStart: 1, /*以星期一为一星期开始*/
        todayBtn:  1,
        autoclose: 1,
        minView:2, /*精确到天*/
        pickerPosition: "bottom-left"
    }).on("changeDate",function(ev){  //值改变事件
        //选择的日期不能大于第二个日期控件的日期
        if(ev.date){
            $("#datetimeEnd").datetimepicker('setStartDate', new Date(ev.date.valueOf()));
        }else{
            $("#datetimeEnd").datetimepicker('setStartDate',null);
        }
    });
    $('#datetimeEnd').datetimepicker({
        language:  'zh-CN',
        format:'yyyy-mm-dd',
        weekStart: 1, /*以星期一为一星期开始*/
        todayBtn:  1,
        autoclose: 1,
        minView:2, /*精确到天*/
        pickerPosition: "bottom-left"
    }).on("changeDate",function(ev){
        /*选择的日期不能小于第一个日期控件的日期*/
        if(ev.date){
            $("#datetimeStart").datetimepicker('setEndDate', new Date(ev.date.valueOf()));
        }else{
            $("#datetimeStart").datetimepicker('setEndDate',new Date());
        }
    });

    var today = new Date();
    today.setHours(0);
    today.setMinutes(0);
    today.setSeconds(0);
    today.setMilliseconds(0);

    $("#datetimeStart").val(Format(today, "yyyy-MM-dd"));
    $("#datetimeEnd").val(Format(new Date(), "yyyy-MM-dd"));

    table3 = $('#datatables').DataTable({
        "searching": false,
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "serverSide": true,

        ajax: function (data, callback, settings) {
            //封装请求参数
            var param = getQueryCondition(data);

            $.ajax({
                type: "GET",
                url: '/agent/profit_details',
                cache: false,  //禁用缓存
                data: param,    //传入已封装的参数
                dataType: "json",
                success: function (result) {
                    callback(result.data);
                }
            });
        },
        "columns": [
            {"data": "id"},
            {"data": "phone"},
            {"data": "realName"},
            {"data": "routeName"},
            {
                "sClass": "text-center",
                "data": "amountTotal",
                "render": function (data, type, full, meta) {
                    return validate(data)+"元";
                },
                "bSortable": false
            },
            {
                "sClass": "text-center",
                "data": "payTime",
                "render": function (data, type, full, meta) {
                    return formatDate(validate(data));
                },
                "bSortable": false
            },
        ],
        columnDefs: [
            {"orderable": false, "targets": 1},
            {"orderable": false, "targets": 2},
            {"orderable": false, "targets": 3},
            {"orderable": false, "targets": 4},
            {"orderable": false, "targets": 5},
        ],

    });
});

function search() {
    table3.ajax.reload();
}
//封装查询参数
function getQueryCondition(data) {
    var param = {};
    param.phone = $("#phone").val();//查询条件
    param.datetimeStart = new Date($("#datetimeStart").val()).getTime();//查询条件
    param.datetimeEnd = new Date($("#datetimeEnd").val()).getTime();//查询条件
    //组装分页参数
    param.start = data.start;
    param.length = data.length;
    param.draw = data.draw;
    return param;
}






//更新数据
function getData(pageNo,length) {
    $.ajax({
        url: "/mall/orderList",
        type: "POST",
        dataType: "json",
        data:getSearch(pageNo,length),
        success:function (json) {
            var str="";
            var dataList = json.data.list;
            createPageNav({
                $container : $("#pageing"),
                pageCount :json.data.pages,
                currentNum : json.data.pageNum,
                afterFun : function(num){
                    getData(num,6);
                },
                hasCommonPage:false
            });

            for (var i = 0; i < dataList.length; i++) {
                var routeType =dataList[i].routeType;
                var status = dataList[i].orderStatus;
                if (routeType ==1){routeType="省内活动订单"}
                if (routeType ==2){routeType="省外活动订单"}
                if (routeType ==3){routeType="国外活动订单"}
                if (status ==1){status="已付款"}
                if (status ==2){status="未付款"}
                var date = "";
                date = new Date(dataList[i].gmtModify);
                str += "<tr>"
                    + "<th>" + dataList[i].orderNo + "</th>"
                    + "<th>" + dataList[i].routeName + "</th>"
                    + "<th>" + routeType + "</th>"
                    + "<th>" + validate(dataList[i].realName) + "</th>"
                    + "<th>" + dataList[i].phone + "</th>"
                    + "<th>" + status + "</th>"
                    + "<th>" + formatDate(dataList[i].createTime)+ "</th>"
                    + "</tr>"
            }
            $('#list').html(str);
        },
        error:function () {
            alert("操作失败");
        }
    })
}


//获取检索条件
function getSearch(pageNo,length) {
    var orderNo = $('[name="orderNo"]').val();
    var routeType = $('[name="routeType"]').val();
    var status = $('[name="status"]').val();
    var starttime = $('[name="starttime"]').val();
    var endtime = $('[name="endtime"]').val();
    var parm={pageNo:pageNo,length:length,orderNo:orderNo,status:status,routeType:routeType,starttime:starttime,endtime:endtime};
    return parm;
}
//时间戳转系统时间格式
function formatDate(now) {
    if (now==""){return "";}
    return new Date(now).toLocaleString();
}
//null转""
function validate(data) {
    if(data==null){
        return "";
    }
    return data;
}